#ifndef __CURSOR_H__
#define __CURSOR_H__

#define MAX_X 57
#define MIN_X 1
#define MAX_Y 29
#define MIN_Y 1
#define X_SKIP 4
#define Y_SKIP 2

#include <iostream>
#include <functional>

using namespace std;

class Cursor
{
public:
	enum Direction
	{
		UP,
		DOWN,
		LEFT,
		RIGHT
	};

	Cursor()
	{
		x = (MAX_X - MIN_X) / 2 + 1;
		y = (MAX_Y - MIN_Y) / 2 + 1;
		show();
	}

	void show() const
	{
		fprintf(stderr, "\033[%d;%dH", y, x);
	}

	void moveUp()
	{
		if (y <= MIN_Y)
		{
			return;
		}
		y -= Y_SKIP;
	}

	void moveDown()
	{
		if (y >= MAX_Y)
		{
			return;
		}
		y += Y_SKIP;
	}

	void moveLeft()
	{
		if (x <= MIN_X)
		{
			return;
		}
		x -= X_SKIP;
	}

	void moveRight()
	{
		if (x >= MAX_X)
		{
			return;
		}
		x += X_SKIP;
	}

	void move(Direction d)
	{
		auto handle = func[d];
		handle();
		show();
	}

	int getX() const
	{
		return x;
	}

	int getY() const
	{
		return y;
	}

	void setPos(int x, int y)
	{
		this->x = x;
		this->y = y;
	}

private:
	int x;
	int y;
	function<void(void)> func[4] = {
		bind(&Cursor::moveUp, this),
		bind(&Cursor::moveDown, this),
		bind(&Cursor::moveLeft, this),
		bind(&Cursor::moveRight, this)
	};
};

#endif // !__CURSOR_H__
